package com.horoscope.astrology.zodiac.signs.free.adapter

import android.os.Bundle
import android.os.Parcelable
import android.util.SparseArray
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.DataResultBean
import com.horoscope.astrology.zodiac.signs.free.fragment.SubFragment
import com.jeremyliao.liveeventbus.LiveEventBus

class SubPagerAdapter(fm: FragmentManager, val size: Int, data: DataResultBean?) :
    FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {


    private val titleList = arrayOf("Today", "Tomorrow", "Month", "Year")
    private val sparseArray = SparseArray<Fragment>()
    private val mData = data
    override fun getItem(position: Int): Fragment {
        return if (sparseArray[position] == null) {
            val fragment = SubFragment()
            val bundle = Bundle()
            bundle.putInt("position", position)
            bundle.putString("titleOfList", titleList[position])
            fragment.arguments = bundle
            sparseArray.put(position, fragment)
            LiveEventBus.get("subFragment", DataResultBean::class.java).post(mData)
            fragment
        } else {
            sparseArray[position]
        }
    }

    override fun getCount(): Int {
        return size
    }


    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {

    }

    override fun getPageTitle(position: Int): CharSequence? {
        return titleList[position]
    }

    override fun saveState(): Parcelable? {
        return null
    }
}